﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelperModels.Common
{
    public class CopyFiles
    { 
        public FileResult CopyMusicToDic(List<MusicHelper> musics,string fileName,string Time)
        {
            Time = Convert.ToDateTime(Time).ToString("yyyyMMddhhmmss");
            FileResult result = new FileResult();
            try
            {
                result.SuccessCount = 0;
                string TotalPath = $@"D:\Orders\{fileName}";
                if (Directory.Exists(TotalPath) == false)
                {
                    Directory.CreateDirectory(TotalPath);
                }
                string CopyPath = $@"D:\Orders\{fileName}\{Time}";
                if (Directory.Exists(CopyPath) == false)
                {
                    Directory.CreateDirectory(CopyPath);
                }
                for (int i = 0; i < musics.Count; i++)
                {
                    if (File.Exists(musics[i].Musics.MusicUrl))//必须判断要复制的文件是否存在
                    {
                        FileInfo file = new FileInfo(musics[i].Musics.MusicUrl);
                        File.Copy(musics[i].Musics.MusicUrl, CopyPath+"\\"+file.Name, false);//三个参数分别是源文件路径，存储路径，若存储路径有相同文件是否替换
                        result.SuccessCount++;
                    }
                };
            }
            catch (Exception ex)
            {
                result.ErrorName.Add(ex.Message+"|"+ex.StackTrace);
               
            }          
            return result;
           
        }
        public FileResult CopyMusicToDicBySort(List<MusicHelper> musics, string fileName, string Time)
        {
            Time = Convert.ToDateTime(Time).ToString("yyyyMMddHHmmss");
            FileResult result = new FileResult();
            try
            {
                result.SuccessCount = 0;
                string TotalPath = $@"D:\Orders\{fileName}";
                if (Directory.Exists(TotalPath) == false)
                {
                    Directory.CreateDirectory(TotalPath);
                }
                string CopyPath = $@"D:\Orders\{fileName}\{Time}";
                if (Directory.Exists(CopyPath) == false)
                {
                    Directory.CreateDirectory(CopyPath);
                }
                for (int i = 0; i < musics.Count; i++)
                {
                    if (File.Exists(musics[i].Musics.MusicUrl))//必须判断要复制的文件是否存在
                    {
                        FileInfo file = new FileInfo(musics[i].Musics.MusicUrl);
                        File.Copy(musics[i].Musics.MusicUrl, CopyPath + $"\\{i+1}." + file.Name, false);//三个参数分别是源文件路径，存储路径，若存储路径有相同文件是否替换
                        result.SuccessCount++;
                    }
                };
            }
            catch (Exception ex)
            {
            }
            return result;

        }
    }
}
